﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;

namespace VCN.admin
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                LoadData();
                Page.Title = "Danh sách tin tức";
            }
        }
        private void LoadData()
        {

            NewsDAO v_NewsDAO = new NewsDAO();
            DataTable v_dt_News = new DataTable();
            v_dt_News = v_NewsDAO.selectAll2Dt();
            if (v_dt_News.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Chưa có dữ liệu !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            dgv_News.DataSource = v_dt_News;
            dgv_News.DataBind();
        }

        protected void gridNews_Delete(object sender, GridViewDeleteEventArgs e)
        {
            NewsDAO v_NewsDAO = new NewsDAO();
            VCN.App.DataTransferObjects.News v_News = new VCN.App.DataTransferObjects.News();
            v_News.Id = int.Parse(dgv_News.DataKeys[e.RowIndex].Value.ToString());
            v_NewsDAO.delete(v_News);
            this.LoadData();
        }

        protected void gridNews_Edit(object sender, GridViewEditEventArgs e)
        {
            string v_id = dgv_News.DataKeys[e.NewEditIndex].Value.ToString();
            Response.Redirect(string.Format("NewsManagement.aspx?Id={0}", v_id));

        }


        protected void dgv_News_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {

                dgv_News.PageIndex = e.NewPageIndex;
                NewsDAO v_NewsDAO = new NewsDAO();
                dgv_News.DataSource = v_NewsDAO.selectAll2Dt();
                dgv_News.DataBind();
                
            }
            catch (Exception ip_e)
            {
                ip_e.ToString();
            }
        }

        protected void Search(object sender, EventArgs e)
        {
            NewsDAO v_NewsDAO = new NewsDAO();
            DataTable v_dt_News = new DataTable();
            v_dt_News = v_NewsDAO.searchNews(string.Concat("%", txt_KeyWord.Text, "%"));
            if (v_dt_News.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Không có kết quả nào liên quan đến từ khóa trên !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            dgv_News.DataSource = v_dt_News;
            dgv_News.DataBind();
        }
        public string GetAuthorName(object ip_int_UsersId)
        {
            // string NewsName = "";
            UsersDAO v_UsersDAO = new UsersDAO();
            return v_UsersDAO.SelectById(int.Parse(ip_int_UsersId.ToString())).UserName;
        }
        public string GetShortContent(object ip_str_ShortContent)
        {
            string v_str_ShortContent=ip_str_ShortContent.ToString();
            if (v_str_ShortContent.Length > 100) return v_str_ShortContent= v_str_ShortContent.Substring(0, 99);
            return v_str_ShortContent;
        }

        protected void ThemMoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNews.aspx");
        }
    }
}
